package com.sdk;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.qo.qsdk.QSDK;
import com.qo.qsdk.config.QSDKLanguage;
import com.qo.qsdk.listener.CustomerResultListener;
import com.qo.qsdk.listener.ExitResultListener;
import com.qo.qsdk.listener.FacebookLikeResultListener;
import com.qo.qsdk.listener.GuestBindResultListener;
import com.qo.qsdk.listener.HistoryResultListener;
import com.qo.qsdk.listener.InitResultListener;
import com.qo.qsdk.listener.LoginResultListener;
import com.qo.qsdk.listener.LogoutResultListener;
import com.qo.qsdk.listener.PayResultListener;
import com.qo.qsdk.listener.ReviewResultListener;
import com.qo.qsdk.listener.SubmitRoleResultListener;
import com.qo.qsdk.listener.SwitchAccountResultListener;
import com.qo.qsdk.listener.SwitchLanguageListener;
import com.qo.qsdk.param.GameRoleParams;
import com.qo.qsdk.param.PayParams;

//FragmentActivity
public class MainActivity extends Activity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        QSDK.getInstance().onCreate(this, savedInstanceState);

        QSDK.getInstance().init(this, new InitResultListener() {
            @Override
            public void onSuccess() {
                toast("初始化成功");
            }

            @Override
            public void onError(int code, String msg) {
                toast("初始化失败："+code+ ",msg:" + msg);
            }
        });

        init();
    }

    int i = 0;
    private void init() {
        findViewById(R.id.login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QSDK.getInstance().login(MainActivity.this, new LoginResultListener() {
                    @Override
                    public void onSuccess(String token) {
                        toast("登录成功：" + token);
                    }

                    @Override
                    public void onCancel() {
                        toast("登录取消");
                    }

                    @Override
                    public void onError(int code, String msg) {
                        toast("登录失败："+code+ ",msg:" + msg);
                    }
                });
            }
        });


        findViewById(R.id.c).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QSDK.getInstance().switchLanguage(MainActivity.this, QSDKLanguage.values()[i%3], new SwitchLanguageListener() {
                    @Override
                    public void onSuccess() {
                        toast("切换成功");
                    }

                    @Override
                    public void onError(int code, String msg) {
                        toast("切换失败："+code+ ",msg:" + msg);
                    }
                });
                i ++;
            }
        });

        findViewById(R.id.his).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QSDK.getInstance().showHistoryAccount(MainActivity.this, new HistoryResultListener() {
                    @Override
                    public void onSuccess(String token) {
                        toast("历史登录成功：" + token);
                    }

                    @Override
                    public void onClose() {
                        toast("历史取消");
                    }

                    @Override
                    public void onError(int code, String msg) {
                        toast("历史失败："+code+ ",msg:" + msg);
                    }
                });
            }
        });

        findViewById(R.id.pay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PayParams p = new PayParams();
                p.setProductId("com.ios.djs.vipgoods7");
                p.setOrderNo(System.currentTimeMillis() + "-order");
                QSDK.getInstance().pay(MainActivity.this, p, new PayResultListener() {
                    @Override
                    public void onSuccess() {
                        toast("支付成功");
                    }

                    @Override
                    public void onError(int code, String msg) {
                        toast("支付失败："+code+ ",msg:" + msg);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
            }
        });

        findViewById(R.id.change).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QSDK.getInstance().switchAccount(MainActivity.this, new SwitchAccountResultListener() {
                    @Override
                    public void onSuccess(String token) {
                        toast("切换账号成功：" + token);
                    }

                    @Override
                    public void onCancel() {
                        toast("切换账号取消");
                    }

                    @Override
                    public void onError(int code, String msg) {
                        toast("切换账号失败："+code+ ",msg:" + msg);
                    }
                });
            }
        });

        findViewById(R.id.logout).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QSDK.getInstance().logout(MainActivity.this, new LogoutResultListener() {
                    @Override
                    public void onSuccess() {
                        toast("注销成功");
                    }

                    @Override
                    public void onError(int code, String msg) {
                        toast("注销失败："+code+ ",msg:" + msg);
                    }
                });
            }
        });

        findViewById(R.id.guestBind).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QSDK.getInstance().showBind(MainActivity.this, new GuestBindResultListener() {
                    @Override
                    public void onSuccess(String token) {
                        //当前版本该处的token可以忽略，直接给用户发放奖励即可
                        toast("游客绑定成功:" + token);
                    }

                    @Override
                    public void onClose() {
                        toast("游客绑定取消");
                    }

                    @Override
                    public void onError(int code, String msg) {
                        toast("游客绑定失败："+code+ ",msg:" + msg);
                    }
                });
            }
        });

        findViewById(R.id.active).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toast("活跃状态：" + (QSDK.getInstance().isActive(MainActivity.this) ? "活跃" : "非活跃"));
            }
        });

        findViewById(R.id.isGuest).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                toast("是否游客：" + (QSDK.getInstance().isGuest(MainActivity.this) ? "是" : "否"));
            }
        });

        findViewById(R.id.submit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QSDK.getInstance().submitGameRoleInfo(MainActivity.this, new GameRoleParams(), new SubmitRoleResultListener() {
                    @Override
                    public void onSuccess() {
                        toast("上报成功");
                    }

                    @Override
                    public void onError(int code, String msg) {
                        toast("上报失败："+code+ ",msg:" + msg);
                    }
                });
            }
        });

        findViewById(R.id.exit).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QSDK.getInstance().exitGame(MainActivity.this, new ExitResultListener() {
                    @Override
                    public void onSuccess() {
                        toast("退出游戏成功");
                    }

                    @Override
                    public void onError(int code, String msg) {
                        toast("退出游戏失败："+code+ ",msg:" + msg);
                    }

                    @Override
                    public void onClose() {
                        toast("取消退出游戏");
                    }
                });
            }
        });

        findViewById(R.id.review).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QSDK.getInstance().showReview(MainActivity.this, new ReviewResultListener() {
                    @Override
                    public void onSuccess() {
                        toast("评价成功");
                    }

                    @Override
                    public void onError(int code, String msg) {
                        toast("评价失败："+code+ ",msg:" + msg);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
            }
        });

        findViewById(R.id.service).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QSDK.getInstance().showService(MainActivity.this, new CustomerResultListener() {
                    @Override
                    public void onSuccess() {
                        toast("打开客服成功");
                    }

                    @Override
                    public void onError(int code, String msg) {
                        toast("打开客服失败："+code+ ",msg:" + msg);
                    }
                });
            }
        });

        findViewById(R.id.fblike).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QSDK.getInstance().showLike(MainActivity.this, new FacebookLikeResultListener() {
                    @Override
                    public void onSuccess() {
                        toast("打开facebookLike成功");
                    }

                    @Override
                    public void onError(int code, String msg) {
                        toast("打开facebookLike失败："+code+ ",msg:" + msg);
                    }
                });
            }
        });

        findViewById(R.id.show).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QSDK.getInstance().showAssistor(MainActivity.this);
            }
        });

        findViewById(R.id.hide).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QSDK.getInstance().hideAssistor(MainActivity.this);
            }
        });
    }

    private void toast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onStart() {
        super.onStart();
        QSDK.getInstance().onStart(MainActivity.this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        QSDK.getInstance().onResume(MainActivity.this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        QSDK.getInstance().onPause(MainActivity.this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        QSDK.getInstance().onStop(MainActivity.this);
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        QSDK.getInstance().onRestart(MainActivity.this);
    }

    @Override
    protected void onDestroy() {
        QSDK.getInstance().onDestroy(MainActivity.this);
        super.onDestroy();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        QSDK.getInstance().onNewIntent(MainActivity.this, intent);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        QSDK.getInstance().onKeyDown(this, keyCode, event);
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        QSDK.getInstance().onBackPressed(MainActivity.this);
        super.onBackPressed();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        QSDK.getInstance().onActivityResult(this, requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        QSDK.getInstance().onRequestPermissionsResult(MainActivity.this, requestCode, permissions, grantResults);
    }
}
